package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityGoodsDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsExample.Criteria;
import com.ecoolex.ying.space.entity.PickGoodsWithBLOBs;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityGoodsDTO;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityGoodsManager {
	
    @Autowired
    private EstateProjectActivityGoodsDAO dao;
    
    @Autowired
    private PickGoodsManager pickGoodsManager;
    
    @Autowired
    private EstateProjectActivityHouseModelItemsGoodsManager itemsGoodsManager;
    
    public EstateProjectActivityGoods getEstateProjectActivityGoods(Integer activityGoodsId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(activityGoodsId, EstateProjectActivityGoods.NOT_DELETED);
	}
    
    public EstateProjectActivityGoods getEstateProjectActivityGoodsOnline(Integer activityGoodsId) {
    	EstateProjectActivityGoodsExample example = new EstateProjectActivityGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andActivityGoodsIdEqualTo(activityGoodsId).andDeletedEqualTo(EstateProjectActivityGoods.NOT_DELETED).andOnlineEqualTo(true);
		return dao.selectOneByExampleWithBLOBs(example);
	}
    
    public EstateProjectActivityGoods getByPickGoodsIdAndActivityCategoryId(Integer pickGoodsId, Integer activityCategoryId) {
    	EstateProjectActivityGoodsExample example = new EstateProjectActivityGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andPickGoodsIdEqualTo(pickGoodsId).andActivityCategoryIdEqualTo(activityCategoryId).andDeletedEqualTo(EstateProjectActivityGoods.NOT_DELETED);
    	return dao.selectOneByExampleWithBLOBs(example);
	}

	public Pagination<EstateProjectActivityGoods> queryEstateProjectActivityGoods(String activityName,Integer activityCategoryId,Boolean online,Sorting[] sorts, Page page) {
		EstateProjectActivityGoodsExample example = new EstateProjectActivityGoodsExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNull(activityName)) {
			criteria.andActivityNameLike("%"+activityName+"%");
		}
		if(Check.notNull(activityCategoryId)) {
			criteria.andActivityCategoryIdEqualTo(activityCategoryId);
		}
		if(Check.notNull(online)) {
			criteria.andOnlineEqualTo(online);
		}
		criteria.andDeletedEqualTo(EstateProjectActivityGoods.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public Pagination<EstateProjectActivityGoods> queryEstateProjectActivityGoodsForPrimaryCategory(String activityName, Integer activityCategoryId, Boolean online, Sorting[] sorts, Page page) {
		
		return Pagination.create(page, 
			dao.countByPrimaryCaterogy(activityName, activityCategoryId, online),
			() -> dao.queryByPrimaryCaterogy(activityName, activityCategoryId, online, Sortings.builder(sorts).toSql(), page));
	}
	
	public Pagination<EstateProjectActivityGoods> queryEstateProjectActivityGoodsByCategoryId(Integer activityCategoryId, Page page){
		EstateProjectActivityGoodsExample example = new EstateProjectActivityGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andDeletedEqualTo(EstateProjectActivityGoods.NOT_DELETED)
				.andActivityCategoryIdEqualTo(activityCategoryId)
				.andOnlineEqualTo(true);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.weight.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public EstateProjectActivityGoods addEstateProjectActivityGoods(AddEstateProjectActivityGoodsDTO record, String createUser){
		PickGoodsWithBLOBs pickGoods = pickGoodsManager.getById(record.getPickGoodsId());
		EstateProjectActivityGoods activityGoods = new EstateProjectActivityGoods();
		activityGoods.setPickGoodsId(pickGoods.getPickGoodsId());
		activityGoods.setActivityCategoryId(record.getActivityCategoryId());
		activityGoods.setWeight(record.getWeight());
		activityGoods.setLink(record.getLink());
		activityGoods.setFreightDescribe(record.getFreightDescribe());
		activityGoods.setServiceDescribe(record.getServiceDescribe());
		activityGoods.setActivityName(pickGoods.getName());
		activityGoods.setActivityCoverImage(pickGoods.getImage());
		activityGoods.setImages(pickGoods.getShopwindowImages());
		activityGoods.setActivityPrice(pickGoods.getRetailPrice());
		activityGoods.setCounterPrice(pickGoods.getCounterPrice());
		activityGoods.setDetail(pickGoods.getDetail());
		activityGoods.setOnline(false);
		activityGoods.setDeleted(EstateProjectActivityGoods.NOT_DELETED);
		activityGoods.setCreateUser(createUser);
		activityGoods.setCreateTime(Date.from(Instant.now()));
		activityGoods.setUpdateUser(createUser);
		activityGoods.setUpdateTime(Date.from(Instant.now()));
		dao.insert(activityGoods);
		return activityGoods;
	}

	public EstateProjectActivityGoods editEstateProjectActivityGoods(EstateProjectActivityGoods record, String updateUser) {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.online,Column.pickGoodsId,Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

	public void deleteEstateProjectActivityGoods(EstateProjectActivityGoods record, String updateUser){
		record.setDeleted(EstateProjectActivityGoods.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
		//删除条目关联的活动商品
		itemsGoodsManager.deleteByActivityGoodsId(record.getActivityGoodsId(), updateUser);
	}

	public void onlineEstateProjectActivityGoods(EstateProjectActivityGoods record, String updateUser){
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}

	public void offlineEstateProjectActivityGoods(EstateProjectActivityGoods record, String updateUser){
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}
}